home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 4.iso / public / fax / src / libtiff / tif_apple.c < prev    next >
C/C++ Source or Header  |  1994-08-01  |  5KB  |  221 lines

  1. #ifndef lint
  2. static char rcsid[] = "$Header: /usr/people/sam/tiff/libtiff/RCS/tif_apple.c,v 1.10 92/10/30 11:43:23 sam Rel $";
  3. #endif
  4.  
  5. /*
  6.  * Copyright (c) 1988, 1989, 1990, 1991, 1992 Sam Leffler
  7.  * Copyright (c) 1991, 1992 Silicon Graphics, Inc.
  8.  *
  9.  * Permission to use, copy, modify, distribute, and sell this software and 
  10.  * its documentation for any purpose is hereby granted without fee, provided
  11.  * that (i) the above copyright notices and this permission notice appear in
  12.  * all copies of the software and related documentation, and (ii) the names of
  13.  * Sam Leffler and Silicon Graphics may not be used in any advertising or
  14.  * publicity relating to the software without the specific, prior written
  15.  * permission of Sam Leffler and Silicon Graphics.
  16.  * 
  17.  * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND, 
  18.  * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY 
  19.  * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.  
  20.  * 
  21.  * IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR
  22.  * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
  23.  * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
  24.  * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF 
  25.  * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE 
  26.  * OF THIS SOFTWARE.
  27.  */
  28.  
  29. /*
  30.  * TIFF Library Macintosh-specific Routines.
  31.  *
  32.  * These routines use only the Toolbox and high level File Manager traps.
  33.  * They make no calls to the THINK C "unix" compatibility library.  Also,
  34.  * malloc is not used directly but it is still referenced internally by
  35.  * in the ANSI library in rare cases.  Heap fragmentation by the malloc ring
  36.  * buffer is therefore minimized.
  37.  *
  38.  * O_RDONLY and O_RDWR are treated identically here.  The tif_mode flag is
  39.  * checked in TIFFWriteCheck().
  40.  *
  41.  * Create below fills in a blank creator signature and sets the file type
  42.  * to 'TIFF'.  It is much better for the application to do this by Create'ing
  43.  * the file first and TIFFOpen'ing it later.
  44.  */
  45.  
  46. #include "tiffiop.h"
  47. #include <Errors.h>
  48. #include <Files.h>
  49. #include <Memory.h>
  50.  
  51. #ifdef applec
  52. #define    CtoPstr    c2pstr
  53. #endif
  54.  
  55. static int
  56. _tiffReadProc(void *fd, char* buf, unsigned long size)
  57. {
  58.     return (FSRead((short) fd, (long *) &size, buf) == noErr ? size : -1);
  59. }
  60.  
  61. static int
  62. _tiffWriteProc(void *fd, char* buf, unsigned long size)
  63. {
  64.     return (FSWrite((short) fd, (long *) &size, buf) == noErr ? size : -1);
  65. }
  66.  
  67. static long
  68. _tiffSeekProc(void *fd, long off, int whence)
  69. {
  70.     long fpos, size;
  71.  
  72.     if (GetEOF((short) fd, &size) != noErr)
  73.         return EOF;
  74.     (void) GetFPos((short) fd, &fpos);
  75.  
  76.     switch (whence) {
  77.     case SEEK_CUR:
  78.         if (off + fpos > size)
  79.             SetEOF((short) fd, off + fpos);
  80.         if (SetFPos((short) fd, fsFromMark, off) != noErr)
  81.             return EOF;
  82.         break;
  83.     case SEEK_END:
  84.         if (off > 0)
  85.             SetEOF((short) fd, off + size);
  86.         if (SetFPos((short) fd, fsFromLEOF, off) != noErr)
  87.             return EOF;
  88.         break;
  89.     case SEEK_SET:
  90.         if (off > size)
  91.             SetEOF((short) fd, off);
  92.         if (SetFPos((short) fd, fsFromLEOF, off - size) != noErr)
  93.             return EOF;
  94.         break;
  95.     }
  96.  
  97.     return (GetFPos((short) fd, &fpos) == noErr ? fpos : EOF);
  98. }
  99.  
  100. static int
  101. _tiffMapProc(void *fd, char **pbase, long *psize)
  102. {
  103.     return (0);
  104. }
  105.  
  106. static void
  107. _tiffUnmapProc(void *fd, char *base, long size)
  108. {
  109. }
  110.  
  111. static int
  112. _tiffCloseProc(void *fd)
  113. {
  114.     return (FSClose((short) fd));
  115. }
  116.  
  117. static long
  118. _tiffSizeProc(void *fd)
  119. {
  120.     long size;
  121.  
  122.     if (GetEOF((short) fd, &size) != noErr) {
  123.         TIFFError("_tiffSizeProc", "%s: Cannot get file size");
  124.         return (-1L);
  125.     }
  126.     return (size);
  127. }
  128.  
  129. /*
  130.  * TODO: use Multifinder temporary memory for large strip chunks.
  131.  */
  132. void *
  133. _TIFFmalloc(size_t s)
  134. {
  135.     return (NewPtr(s));
  136. }
  137.  
  138. void
  139. _TIFFfree(void* p)
  140. {
  141.     DisposePtr(p);
  142. }
  143.  
  144. void *
  145. _TIFFrealloc(void* p, size_t s)
  146. {
  147.     Ptr n = p;
  148.  
  149.     SetPtrSize(p, s);
  150.     if (MemError() && (n = NewPtr(s)) != NULL) {
  151.         BlockMove(p, n, GetPtrSize(p));
  152.         DisposePtr(p);
  153.     }
  154.     return (n);
  155. }
  156.  
  157. /*
  158.  * Open a TIFF file descriptor for read/writing.
  159.  */
  160. TIFF *
  161. TIFFFdOpen(int fd, const char *name, const char *mode)
  162. {
  163.     TIFF *tif;
  164.  
  165.     tif = TIFFClientOpen(name, mode, (void *) fd,
  166.         _tiffReadProc, _tiffWriteProc, _tiffSeekProc, _tiffCloseProc,
  167.         _tiffSizeProc, _tiffMapProc, _tiffUnmapProc);
  168.     if (tif)
  169.         tif->tif_fd = fd;
  170.     return (tif);
  171. }
  172.  
  173. /*
  174.  * Open a TIFF file for read/writing.
  175.  */
  176. TIFF *
  177. TIFFOpen(const char *name, const char *mode)
  178. {
  179.     static const char module[] = "TIFFOpen";
  180.     Str255 pname;
  181.     FInfo finfo;
  182.     short fref;
  183.     OSErr err;
  184.  
  185.     strcpy((char *) pname, name);
  186.     CtoPstr(pname);
  187.  
  188.     switch (_TIFFgetMode(mode, module)) {
  189.     default:
  190.         return ((TIFF *) 0);
  191.     case O_RDWR | O_CREAT | O_TRUNC:
  192.         if (GetFInfo(pname, 0, &finfo) == noErr)
  193.             FSDelete(pname, 0);
  194.         /* fall through */
  195.     case O_RDWR | O_CREAT:
  196.         if ((err = GetFInfo(pname, 0, &finfo)) == fnfErr) {
  197.             if (Create(pname, 0, '    ', 'TIFF') != noErr)
  198.                 goto badCreate;
  199.             if (FSOpen(pname, 0, &fref) != noErr)
  200.                 goto badOpen;
  201.         } else if (err == noErr) {
  202.             if (FSOpen(pname, 0, &fref) != noErr)
  203.                 goto badOpen;
  204.         } else
  205.             goto badOpen;
  206.         break;
  207.     case O_RDONLY:
  208.     case O_RDWR:
  209.         if (FSOpen(pname, 0, &fref) != noErr)
  210.             goto badOpen;
  211.         break;
  212.     }
  213.     return (TIFFFdOpen((int) fref, name, mode));
  214. badCreate:
  215.     TIFFError(module, "%s: Cannot create", name);
  216.     return ((TIFF *) 0);
  217. badOpen:
  218.     TIFFError(module, "%s: Cannot open", name);
  219.     return ((TIFF *) 0);
  220. }
  221.